package com.supermarket.view;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.swt.SWTResourceManager;

import com.supermarket.Dao.VipCardDao;
import com.supermarket.util.Dbutil;

import java.sql.Connection;
import java.sql.ResultSet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swtchart.Chart;
import org.eclipse.swtchart.IAxis;
import org.eclipse.swtchart.IAxisSet;
import org.eclipse.swtchart.IAxisTick;
import org.eclipse.swtchart.IBarSeries;
import org.eclipse.swtchart.IGrid;
import org.eclipse.swtchart.ILegend;
import org.eclipse.swtchart.ILineSeries;
import org.eclipse.swtchart.ISeries.SeriesType;
import org.eclipse.swtchart.ISeriesLabel;
import org.eclipse.swtchart.ITitle;
import org.eclipse.swtchart.LineStyle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;


public class Peoplechart extends Composite {
	private VipCardDao vipcarDao=new VipCardDao();
	private Dbutil dbutil=new Dbutil();
	private int a=0;
	private int b=0;
	private int c=0;
	private int d=0;
	private int e=0;
	private double a1=0;
	private double b1=0;
	private double c1=0;
	private double d1=0;
	private double e1=0;
	private Label label_3;
	private Chart chart;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Peoplechart(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(243, 95, 95));
		setForeground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		
		fillchar();
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 23, SWT.NORMAL));
		lblNewLabel.setForeground(SWTResourceManager.getColor(255, 250, 250));
		lblNewLabel.setBackground(SWTResourceManager.getColor(243, 95, 95));
		lblNewLabel.setBounds(30, 23, 281, 65);
		lblNewLabel.setText("\u6D88\u8D39\u4EBA\u7FA4\u7EDF\u8BA1");
		
		Label label = new Label(this, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 43, SWT.BOLD));
		label.setBackground(SWTResourceManager.getColor(243, 95, 95));
		label.setForeground(SWTResourceManager.getColor(255, 250, 250));
		label.setBounds(104, 94, 53, 50);
		label.setText("\u201C");
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_BACKGROUND));
		
		composite.setBounds(175, 130, 772, 546);
		
		
		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_2.setBounds(426, 20, 73, 25);
		label_2.setText("总营收：");
		
		label_3 = new Label(composite, SWT.NONE);
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_LINK_FOREGROUND));
		label_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.NORMAL));
		label_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3.setBounds(505, 15, 221, 30);
		label_3.setText(String.format("%.2f", (a1+b1+c1+d1+e1)*1)+" 元");
		
		// create a chart
		chart = new Chart(composite, SWT.NONE);
		chart.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		chart.setBounds(10, 63, 750, 473);
		
		//设置X,Y轴
		IAxisSet axisSet = chart.getAxisSet();
		
		//刻度颜色
		IAxisTick xTick = axisSet.getXAxis(0).getTick();
		IAxisTick yTick = axisSet.getYAxis(0).getTick();
		Color black = new Color(Display.getDefault(), 0, 0, 0);
		xTick.setForeground(black);
		yTick.setForeground(black);
		
		
		//设置标题，X,Y轴名称颜色
		chart.getTitle().setText("各年龄段人群消费水平");
		chart.getAxisSet().getXAxis(0).getTitle().setText("年龄段 (岁)");
		chart.getAxisSet().getYAxis(0).getTitle().setText("消费水平 (元)");
		
		IAxis xAxis = axisSet.getXAxis(0);
		xAxis.setCategorySeries(new String[] { "0", "20", "40", "60", "80","100" });
		xAxis.enableCategory(true);
		
		chart.getTitle().setForeground(black);
		chart.getAxisSet().getXAxis(0).getTitle().setForeground(black);
		chart.getAxisSet().getYAxis(0).getTitle().setForeground(black);
		
		//网格样式
		IGrid xGrid = axisSet.getXAxis(0).getGrid();
		IGrid yGrid = axisSet.getYAxis(0).getGrid();
		xGrid.setStyle(LineStyle.NONE);
		yGrid.setStyle(LineStyle.SOLID);
		
		// create bar series
		//double[] char1 = {a,b,c,d,e};
		double[] char2 = {a1,b1,c1,d1,e1};
		
		
		IBarSeries barSeries = (IBarSeries) chart.getSeriesSet()
		    .createSeries(SeriesType.BAR, "消费水平");
		barSeries.setYSeries(char2);		
				
		// adjust the axis range
		Color color = new Color(Display.getDefault(), 80, 240, 180);
		barSeries.setBarColor(color);

//		ILineSeries lineSeries = (ILineSeries) chart.getSeriesSet()
//			    .createSeries(SeriesType.LINE, "年龄段人数");
//			lineSeries.setYSeries(char1);
			
		//图例
		ILegend legend = chart.getLegend();
		legend.setPosition(SWT.TOP);
		
		String[] char3 = {"##","##","##","##","##"};
		ISeriesLabel seriesLabel = barSeries.getLabel();
		seriesLabel.setFormats(char3);
		
		// adjust the axis range
		chart.getAxisSet().adjustRange();
					
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setText("\u201C");
		label_1.setForeground(SWTResourceManager.getColor(255, 250, 250));
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 43, SWT.BOLD));
		label_1.setBackground(SWTResourceManager.getColor(243, 95, 95));
		label_1.setBounds(987, 673, 53, 50);

		//System.out.println(a1+" "+b1+" "+c1+" "+d1+" "+e1);
		//System.out.println(a+" "+b+" "+c+" "+d+" "+e);
	}

	private void fillchar(){
		Connection con=null;
		try {
			con=dbutil.getcon();
			ResultSet rs=vipcarDao.Peoplechar(con);
			while(rs.next()) {
				if(rs.getInt("age")<20) {
					a1=rs.getFloat("MSUM")+a1;
					a++;
					continue;
				}
				if(rs.getInt("age")>=20&&rs.getInt("age")<40) {
					b1=rs.getFloat("MSUM")+b1;
					b++;
					continue;
				}
				if(rs.getInt("age")>=40&&rs.getInt("age")<60) {
					c1=rs.getFloat("MSUM")+c1;
					c++;
					continue;
				}
				if(rs.getInt("age")>=60&&rs.getInt("age")<80) {
					d1=rs.getFloat("MSUM")+d1;
					d++;
					continue;
				}
				if(rs.getInt("age")>=80&&rs.getInt("age")<=100){
					e1=rs.getFloat("MSUM")+e1;
					e++;
					continue;
				}
			}
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			try {
				dbutil.closeCon(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
